<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=6.0.4">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.4" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.0.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  
  <meta name="keywords" content="百度IFE2017," />


<meta name="description" content="任务目的在上一任务基础上继续JavaScript的体验接触一下JavaScript中的高级选择器学习JavaScript中的数组对象遍历、读写、排序等操作学习简单的字符串处理操作 任务描述参考以下示例代码，读取页面上已有的source列表，从中提取出城市以及对应的空气质量将数据按照某种顺序排序后，在resort列表中按照顺序显示出来">
<meta name="keywords" content="百度IFE2017">
<meta property="og:type" content="article">
<meta property="og:title" content="IFE JavaScript Task 1-03">
<meta property="og:url" content="http://yoursite.com/2017/06/23/ife2017/js/Task03/index.html">
<meta property="og:site_name" content="Kamui&#39;s Notes">
<meta property="og:description" content="任务目的在上一任务基础上继续JavaScript的体验接触一下JavaScript中的高级选择器学习JavaScript中的数组对象遍历、读写、排序等操作学习简单的字符串处理操作 任务描述参考以下示例代码，读取页面上已有的source列表，从中提取出城市以及对应的空气质量将数据按照某种顺序排序后，在resort列表中按照顺序显示出来">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-02-28T03:43:14.691Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="IFE JavaScript Task 1-03">
<meta name="twitter:description" content="任务目的在上一任务基础上继续JavaScript的体验接触一下JavaScript中的高级选择器学习JavaScript中的数组对象遍历、读写、排序等操作学习简单的字符串处理操作 任务描述参考以下示例代码，读取页面上已有的source列表，从中提取出城市以及对应的空气质量将数据按照某种顺序排序后，在resort列表中按照顺序显示出来">



  <link rel="alternate" href="/atom.xml" title="Kamui's Notes" type="application/atom+xml" />




  <link rel="canonical" href="http://yoursite.com/2017/06/23/ife2017/js/Task03/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>
  <title>IFE JavaScript Task 1-03 | Kamui's Notes</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Kamui's Notes</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
        </li>
      
        
        <li class="menu-item menu-item-project">
          <a href="/project/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-diamond"></i> <br />项目</a>
        </li>
      
        
        <li class="menu-item menu-item-demo">
          <a href="/demo/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />演示</a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
        </li>
      

      
    </ul>
  

  
</nav>


  



 </div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/06/23/ife2017/js/Task03/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kamui">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kamui's Notes">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">IFE JavaScript Task 1-03</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-23T16:19:19+08:00">2017-06-23</time>
            

            
            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon"
            >
            <i class="fa fa-eye"></i>
             阅读次数： 
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="任务目的"><a href="#任务目的" class="headerlink" title="任务目的"></a>任务目的</h2><p>在上一任务基础上继续JavaScript的体验<br>接触一下JavaScript中的高级选择器<br>学习JavaScript中的数组对象遍历、读写、排序等操作<br>学习简单的字符串处理操作</p>
<h2 id="任务描述"><a href="#任务描述" class="headerlink" title="任务描述"></a>任务描述</h2><p>参考以下示例代码，读取页面上已有的<code>source</code>列表，从中提取出城市以及对应的空气质量<br>将数据按照某种顺序排序后，在<code>resort</code>列表中按照顺序显示出来</p>
 <a id="more"></a>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>IFE JavaScript Task 01<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"source"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>北京空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>90<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>上海空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>70<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>天津空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>80<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>广州空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>50<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>深圳空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>40<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>福州空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>32<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>成都空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>90<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"resort"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">    &lt;li&gt;第一名：北京空气质量：&lt;b&gt;90&lt;/b&gt;&lt;/li&gt;</span></span><br><span class="line"><span class="comment">    &lt;li&gt;第二名：北京空气质量：&lt;b&gt;90&lt;/b&gt;&lt;/li&gt;</span></span><br><span class="line"><span class="comment">    &lt;li&gt;第三名：北京空气质量：&lt;b&gt;90&lt;/b&gt;&lt;/li&gt;</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">"sort-btn"</span>&gt;</span>排序<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">/**</span></span><br><span class="line"><span class="undefined"> * getData方法</span></span><br><span class="line"><span class="undefined"> * 读取id为source的列表，获取其中城市名字及城市对应的空气质量</span></span><br><span class="line"><span class="undefined"> * 返回一个数组，格式见函数中示例</span></span><br><span class="line"><span class="undefined"> */</span></span><br><span class="line"><span class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">getData</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined">  /*</span></span><br><span class="line"><span class="undefined">  coding here</span></span><br><span class="line"><span class="undefined">  */</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">  /*</span></span><br><span class="line"><span class="undefined">  data = [</span></span><br><span class="line"><span class="javascript">    [<span class="string">"北京"</span>, <span class="number">90</span>],</span></span><br><span class="line"><span class="javascript">    [<span class="string">"北京"</span>, <span class="number">90</span>]</span></span><br><span class="line"><span class="undefined">    ……</span></span><br><span class="line"><span class="undefined">  ]</span></span><br><span class="line"><span class="undefined">  */</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">return</span> data;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">/**</span></span><br><span class="line"><span class="undefined"> * sortAqiData</span></span><br><span class="line"><span class="undefined"> * 按空气质量对data进行从小到大的排序</span></span><br><span class="line"><span class="undefined"> * 返回一个排序后的数组</span></span><br><span class="line"><span class="undefined"> */</span></span><br><span class="line"><span class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">sortAqiData</span>(<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">/**</span></span><br><span class="line"><span class="undefined"> * render</span></span><br><span class="line"><span class="undefined"> * 将排好序的城市及空气质量指数，输出显示到id位resort的列表中</span></span><br><span class="line"><span class="undefined"> * 格式见ul中的注释的部分</span></span><br><span class="line"><span class="undefined"> */</span></span><br><span class="line"><span class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">btnHandle</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">var</span> aqiData = getData();</span></span><br><span class="line"><span class="undefined">  aqiData = sortAqiData(aqiData);</span></span><br><span class="line"><span class="undefined">  render(aqiData);</span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript"><span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">  <span class="comment">// 在这下面给sort-btn绑定一个点击事件，点击时触发btnHandle函数</span></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">&#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">init();</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="任务注意事项"><a href="#任务注意事项" class="headerlink" title="任务注意事项"></a>任务注意事项</h2><p>实现简单功能的同时，请仔细学习JavaScript基本语法、事件、DOM相关的知识<br>请注意代码风格的整齐、优雅<br>代码中含有必要的注释<br>建议不使用任何第三方库、框架<br>示例代码仅为示例，可以直接使用，也可以完全自己重写</p>
<hr>
<p><em>代码如下：</em></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>IFE JavaScript Task 03<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"source"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>北京空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>90<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>上海空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>70<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>天津空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>80<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>广州空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>50<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>深圳空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>40<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>福州空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>32<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>成都空气质量：<span class="tag">&lt;<span class="name">b</span>&gt;</span>90<span class="tag">&lt;/<span class="name">b</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"resort"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">    &lt;li&gt;第一名：北京空气质量：&lt;b&gt;90&lt;/b&gt;&lt;/li&gt;</span></span><br><span class="line"><span class="comment">    &lt;li&gt;第二名：北京空气质量：&lt;b&gt;90&lt;/b&gt;&lt;/li&gt;</span></span><br><span class="line"><span class="comment">    &lt;li&gt;第三名：北京空气质量：&lt;b&gt;90&lt;/b&gt;&lt;/li&gt;</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">"sort-btn"</span>&gt;</span>排序<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    /**</span></span><br><span class="line"><span class="undefined">     * getData方法</span></span><br><span class="line"><span class="undefined">     * 读取id为source的列表，获取其中城市名字及城市对应的空气质量</span></span><br><span class="line"><span class="undefined">     * 返回一个数组，格式见函数中示例</span></span><br><span class="line"><span class="undefined">     */</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">getData</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> data = [];</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> getSourceData = <span class="built_in">document</span>.getElementById(<span class="string">"source"</span>).getElementsByTagName(<span class="string">"li"</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i &lt; getSourceData.length; i ++)&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">for</span>(<span class="keyword">var</span> j = <span class="number">0</span>;j &lt; getSourceData.length; j ++) &#123;</span></span><br><span class="line"><span class="undefined">                data[i] = [];</span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> repTxt = getSourceData[i].innerHTML.replace(<span class="regexp">/[^\u4e00-\u9fa5]/gi</span>, <span class="string">""</span>);<span class="comment">//正则表达式匹配城市名称</span></span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> repData = getSourceData[i].innerHTML.replace(<span class="regexp">/[^0-9]+/g</span>, <span class="string">''</span>);          <span class="comment">//正则表达式匹配数值</span></span></span><br><span class="line"><span class="javascript">                data[i].push(repTxt,<span class="built_in">parseInt</span>(repData));</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">        /*</span></span><br><span class="line"><span class="undefined">         data = [</span></span><br><span class="line"><span class="javascript">         [<span class="string">"北京"</span>, <span class="number">90</span>],</span></span><br><span class="line"><span class="javascript">         [<span class="string">"北京"</span>, <span class="number">90</span>]</span></span><br><span class="line"><span class="undefined">         ……</span></span><br><span class="line"><span class="undefined">         ]</span></span><br><span class="line"><span class="undefined">         */</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> data;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    /**</span></span><br><span class="line"><span class="undefined">     * sortAqiData</span></span><br><span class="line"><span class="undefined">     * 按空气质量对data进行从小到大的排序</span></span><br><span class="line"><span class="undefined">     * 返回一个排序后的数组</span></span><br><span class="line"><span class="undefined">     */</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">sortAqiData</span>(<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> array = [];</span></span><br><span class="line"><span class="javascript">        array = data.sort(<span class="function"><span class="keyword">function</span>(<span class="params">x, y</span>)</span>&#123;    <span class="comment">//二维数组排序</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">return</span> x[<span class="number">1</span>] - y[<span class="number">1</span>];</span></span><br><span class="line"><span class="undefined">        &#125;);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">        <span class="comment">//console.log(array);//控制台打印数组</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> array;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined">    /**</span></span><br><span class="line"><span class="undefined">     * render</span></span><br><span class="line"><span class="undefined">     * 将排好序的城市及空气质量指数，输出显示到id位resort的列表中</span></span><br><span class="line"><span class="undefined">     * 格式见ul中的注释的部分</span></span><br><span class="line"><span class="undefined">     */</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">data</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> getListId = <span class="built_in">document</span>.getElementById(<span class="string">"resort"</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> string = <span class="string">""</span>;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">for</span> (i = <span class="number">0</span>; i&lt; data.length; i ++)&#123;</span></span><br><span class="line"><span class="javascript">                string += <span class="string">"&lt;li&gt;"</span> + <span class="string">"第"</span> + (i+<span class="number">1</span>) + <span class="string">"名"</span> + <span class="string">"&amp;nbsp;"</span> +  data[i][<span class="number">0</span>] + <span class="string">"：&lt;b&gt;"</span> + data[i][<span class="number">1</span>] + <span class="string">"&lt;/b&gt;&lt;/li&gt;"</span>;</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined">            getListId.innerHTML = string;</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">btnHandle</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> aqiData = getData();</span></span><br><span class="line"><span class="undefined">        aqiData = sortAqiData(aqiData);</span></span><br><span class="line"><span class="undefined">        render(aqiData);</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> getButtonId = <span class="built_in">document</span>.getElementById(<span class="string">"sort-btn"</span>);</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">        <span class="comment">//绑定事件方法</span></span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="keyword">function</span> <span class="title">addListener</span>(<span class="params">id,ev,fn</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">try</span>&#123;</span></span><br><span class="line"><span class="undefined">                id.addEventListener(ev,fn)</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">catch</span> (e)&#123;</span></span><br><span class="line"><span class="javascript">                id.attachEvent(<span class="string">"on"</span> + ev,fn)</span></span><br><span class="line"><span class="undefined">            &#125;</span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 在这下面给sort-btn绑定一个点击事件，点击时触发btnHandle函数</span></span></span><br><span class="line"><span class="javascript">        addListener(getButtonId,<span class="string">"click"</span>,btnHandle);</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">    init();</span></span><br><span class="line"><span class="undefined"></span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><em>Demo如下：</em></p>
<p> <ul id="source"><li>北京空气质量：<b>90</b></li><li>上海空气质量：<b>70</b></li><li>天津空气质量：<b>80</b></li><li>广州空气质量：<b>50</b></li><li>深圳空气质量：<b>40</b></li><li>福州空气质量：<b>32</b></li><li>成都空气质量：<b>90</b></li></ul><br> <ul id="resort"><br>     <!--
     <li>第一名：北京空气质量：<b>90</b></li>
     <li>第二名：北京空气质量：<b>90</b></li>
     <li>第三名：北京空气质量：<b>90</b></li>
      --><br> </ul><button id="sort-btn">排序</button></p>
 <script type="text/javascript">

     /**
      * getData方法
      * 读取id为source的列表，获取其中城市名字及城市对应的空气质量
      * 返回一个数组，格式见函数中示例
      */
     function getData() {

         var data = [];
         var getSourceData = document.getElementById("source").getElementsByTagName("li");
         for(var i = 0;i < getSourceData.length; i ++){
             for(var j = 0;j < getSourceData.length; j ++) {
                 data[i] = [];
                 var repTxt = getSourceData[i].innerHTML.replace(/[^\u4e00-\u9fa5]/gi, "");//正则表达式匹配城市名称
                 var repData = getSourceData[i].innerHTML.replace(/[^0-9]+/g, '');          //正则表达式匹配数值
                 data[i].push(repTxt,parseInt(repData));
             }
         }

         /*
          data = [
          ["北京", 90],
          ["北京", 90]
          ……
          ]
          */

         return data;
     }

     /**
      * sortAqiData
      * 按空气质量对data进行从小到大的排序
      * 返回一个排序后的数组
      */
     function sortAqiData(data) {
         var array = [];
         array = data.sort(function(x, y){    //二维数组排序
             return x[1] - y[1];
         });

         //console.log(array);//控制台打印数组
         return array;

     }

     /**
      * render
      * 将排好序的城市及空气质量指数，输出显示到id位resort的列表中
      * 格式见ul中的注释的部分
      */
     function render(data) {
         var getListId = document.getElementById("resort");
         var string = "";
             for (i = 0; i< data.length; i ++){
                 string += "<li>" + "第" + (i+1) + "名" + "&nbsp;" +  data[i][0] + "：<b>" + data[i][1] + "</b></li>";
             }
             getListId.innerHTML = string;
     }

     function btnHandle() {
         var aqiData = getData();
         aqiData = sortAqiData(aqiData);
         render(aqiData);
     }

     function init() {

         var getButtonId = document.getElementById("sort-btn");

         //绑定事件方法
         function addListener(id,ev,fn) {
             try{
                 id.addEventListener(ev,fn)
             }
             catch (e){
                 id.attachEvent("on" + ev,fn)
             }
         }

         // 在这下面给sort-btn绑定一个点击事件，点击时触发btnHandle函数
         addListener(getButtonId,"click",btnHandle);
     }
     init();

 </script>

      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/百度IFE2017/" rel="tag"># 百度IFE2017</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/06/22/ife2017/js/Task02/" rel="next" title="IFE JavaScript Task 1-02">
                <i class="fa fa-chevron-left"></i> IFE JavaScript Task 1-02
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/06/29/ife2017/js/Task04/" rel="prev" title="IFE JavaScript Task 1-04">
                IFE JavaScript Task 1-04 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.png"
                alt="Kamui" />
            
              <p class="site-author-name" itemprop="name">Kamui</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">58</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                    <span class="site-state-item-count">1</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">16</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/kamui1073" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#任务目的"><span class="nav-number">1.</span> <span class="nav-text">任务目的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#任务描述"><span class="nav-number">2.</span> <span class="nav-text">任务描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#任务注意事项"><span class="nav-number">3.</span> <span class="nav-text">任务注意事项</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kamui</span>

  

  
</div>




  <div class="powered-by">Hosted by <a href='https://coding.net/help/doc/pages/index.html' style='font-weight:bold;' target='_blank'>Coding Pages</a></div>


<span class="post-meta-divider">|</span>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Muse</a> v6.0.4</div>




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv" title="总访客量">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  
  <span class="post-meta-divider"></span>
  
    <span class="site-pv" title="总访问量">
      <i class="fa fa-spin fa-globe"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.4"></script>




  


  <script type="text/javascript" src="/js/src/love.js?v=6.0.4"></script>

  <script type="text/javascript" src="/js/src/console-log.js?v=6.0.4"></script>




  



	





  





  










  





  

  

  

  

  
  

  

  

  

  



  



</body>
</html>
